package com.android.measuringimportanceoflectureslides;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.android.measuringimportanceoflectureslides.activities.AndroidCamera;
import com.android.measuringimportanceoflectureslides.activities.PreviewActivity;
import com.android.measuringimportanceoflectureslides.activities.Statistics;
import com.android.measuringimportanceoflectureslides.activities.TakePicture;
import com.android.measuringimportanceoflectureslides.builder.ToMakeBuilder;
import com.android.measuringimportanceoflectureslides.data.Database;
import com.android.measuringimportanceoflectureslides.data.SimpleList;
import android.os.Bundle;
import android.preference.Preference;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	
	
	//***********List****************//
	private SimpleList fileNameList;
	
	
	//***********Database************//
	private Database database;
	
	//***********Dialog**************//
	private ToMakeBuilder dialogBuilder;
	
	
	
	//***********Take Picture********//	
	private TakePicture captureSlide;
	private AndroidCamera previewCamera;
	
	//If variable is set to 1, debug is on
	public static int debug = 1;
	public static int debugDB = 0;
	public static int debugCamera = 1;

	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        Log.d(TAG,"called onCreate()");
        
		ListView listView = (ListView) findViewById(R.id.lst_menu_captured_slides);
				
        //***********Database************//
        //Create the database
        database = new Database(MainActivity.this,listView);
        database.databaseOnCreate();
                
        
        //***********List****************//
//		fileNameList = new SimpleList(MainActivity.this, listView);
//		fileNameList.createList();
		
		
		//***********Take Picture********//
		captureSlide = new TakePicture(MainActivity.this, (SurfaceView) findViewById(R.id.layout_xml_preview_surface_view));
//        previewCamera = new AndroidCamera(MainActivity.this);
		
		
		//***********Dialog**************//
//		dialogBuilder = new ToMakeBuilder(MainActivity.this, database);
    }

	
	
    @Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy() aufgerufen");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "onPause() aufgerufen");
		
		//***********Database************//
		//Paus the database
		database.databaseOnPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(TAG, "onRestart() aufgerufen");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "onResume() aufgerufen");
		
		//***********Database************//
		//Resume the database
		database.databaseOnResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "onStart() aufgerufen");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "onStop() aufgerufen");
	}

	
	public void onButtonClick(View view){
		switch(view.getId()){
		case R.id.btn_layout_xml_statistics_xml:
			startActivity(new Intent(this, Statistics.class));
			break;
		
		//************* Preview Button ************	
		case R.id.layout_xml_btn_start_preview:
			startActivity(new Intent(this,PreviewActivity.class));
			break;
			
		case R.id.btn_preferences:
//			startActivity(new Intent(this, Preference.class));
			break;
		}
	}
	
	public void onToggleClicked(View view) {
		switch(view.getId()){
		
			//*********** Capturing Button *************
			case R.id.layout_xml_tgl_btn_capturing:
				// Is the toggle on?
				boolean toggelButtonOn = ((ToggleButton) view).isChecked();
				
				if (toggelButtonOn) {
					// when the toggle is on do this...
					try{
//						dialogBuilder.buildDialog();
						captureSlide.startCapturing();
					}
					catch (Exception e) {
						e.printStackTrace();
						Toast.makeText(this, "exception: " + "\n" + e.getMessage(), Toast.LENGTH_SHORT).show();
					}
				} 
				else {
					// if it is turned of do this...
					captureSlide.stopCapturing();
				}
				break;
		}
	}

}

